package ui.videos 
{
	import com.greensock.TweenMax;
	import com.video.VideoPlayerEvent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import utils.ShapeUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class SeekBar extends MovieClip 
	{
		public var videoPlayer:*;
		private var _barBg:Sprite;
		private var _seekBar:Sprite;
		private var _percent:Number = 0;
		private var _playHeadPercentage:Number=0;
		private var _thumbBtn:MovieClip;
		private var _transparentBtn:Sprite;
		public function SeekBar() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			var sw:Number = stage.stageWidth -322;
			_thumbBtn = new MovieClip();
			var bulle:Shape = ShapeUtil.getCircle(4.5, 0x01f2f2);
			_thumbBtn.addChild(bulle);
			_thumbBtn.buttonMode = true;
			_barBg = ShapeUtil.getSprite(sw, 2, 0xffffff);
			_seekBar = ShapeUtil.getSprite(sw, 4, 0x01f2f2);
			_seekBar.width = 1;
			_thumbBtn.butt
			addChild(_barBg);
			addChild(_seekBar);
			addTransparentBtn();
			addChild(_thumbBtn);
			
			_thumbBtn.addEventListener(MouseEvent.MOUSE_DOWN, _onDown);
			//_thumbBtn.addEventListener(MouseEvent.MOUSE_UP, _onUp);
			resize();
		}
		
		private function addTransparentBtn():void 
		{
			_transparentBtn = ShapeUtil.getSprite(_barBg.width, _barBg.height + 2, 0xff0000, 0);
			_transparentBtn.buttonMode = true;
			_transparentBtn.y = _barBg.y - 1;
			addChild(_transparentBtn);
			_transparentBtn.addEventListener(MouseEvent.MOUSE_DOWN, _onClik);
		}
		
		private function _onClik(e:MouseEvent):void 
		{
			var wtr:Number = ((_transparentBtn.mouseX * 100 ) / _barBg.width);
			videoPlayer.setSeekingPercent(wtr);
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PLAY));
			trace(wtr + '%');
		}
		
		private function _onUp(e:MouseEvent):void 
		{
			trace("[SeekBar._onUp] ");
			
			
			stage.removeEventListener(MouseEvent.MOUSE_UP, _onUp);
			var wtr:Number = ((_seekBar.width*100 ) / _barBg.width);
			videoPlayer.setSeekingPercent(wtr)

			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PLAY));
			//_thumbBtn.stopDrag();
			removeEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
		}
		
		private function _onDown(e:MouseEvent):void 
		{
			trace("[SeekBar._onDown] ");
			dispatchEvent(new VideoPlayerEvent(VideoPlayerEvent.VIDEO_PAUSE));
			stage.addEventListener(MouseEvent.MOUSE_UP, _onUp);
			addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
		}
		
		private function _onMouseMove(e:MouseEvent):void 
		{
			_thumbBtn.y = (_seekBar.height / 2);
			
			var mx:Number = this.mouseX;
			if ( mx > _barBg.x && mx<_barBg.width)
			{
				_thumbBtn.x = mx;
				_seekBar.width = _thumbBtn.x;
				var wtr:Number = ((_barBg.width * _seekBar.width ) / 100);
				//videoPlayer.setSeekingPercent(wtr);
			}
			
		}
		
		
		public function update (playHeadPercentage:Number):void
		{
			_playHeadPercentage = playHeadPercentage;
			var wtr:Number = ((_barBg.width * _playHeadPercentage) / 100);
			_seekBar.width = wtr;
			_thumbBtn.x = wtr;
			//TweenMax.to(_seekBar, .2, { width:wtr } );
			//TweenMax.to(_thumbBtn, .2, { x:wtr } );
			
			//trace("[SeekBar.update] ",wtr);
			//_seekBar.scaleX = _percent;
		}
		
		public function resize():void 
		{
			var sw:Number = stage.stageWidth -322;
			_barBg.width = sw;
			update(_playHeadPercentage);
			
			
			//_seekBar.scaleX = _percent;
			//_seekBar.width = _barBg.width;
			
			_barBg.height = 2;
			_barBg.y = 1;
			_thumbBtn.y = (_seekBar.height / 2);
			
			_transparentBtn.width = sw;
		}
		
	}

}